*Code for Figure 6.6 
*Code Table 6.2, A.15, and A.17

 set more off
*use "C:\Users\Andy baker\OneDrive - UCB-O365\Data\Brazil\BEPS 2014\BEPS 2014 Merged Data v3.dta", clear
use "C:\Users\Andy baker\OneDrive - UCB-O365\Data\Brazil\BEPS 2014\BEPS 2014 Merged Data v3.dta", clear
sort idtelefone wave
merge 1:1 idtelefone wave using "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 6 Regionalism\Data\brazil temp.dta"
drop _m
merge m:1 uf using "C:\Users\Andy Baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 6 Regionalism\Data\nationalization_bra2014_mergetoindy.dta"
drop _m
merge m:1 uf using "C:\Users\Andy Baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 6 Regionalism\Data\nationalization_bra2010_mergetoindy.dta"
*gen ptvspsdb2010=dilma2010/(dilma2010+serra2010)
*gen marinavspsdb2010=marina2010/(marina2010+serra2010)
replace dilma2010=dilma2010/(dilma2010+marina2010+serra2010)
replace serra2010=serra2010/(dilma2010+marina2010+serra2010)
replace marina2010=marina2010/(dilma2010+marina2010+serra2010)

xtset idtelefone wave
gen respond=1

*Before reshaping, generate some variables
	*vote choice
recode vb1 1=1 2=2 3=3 4=4 5/12=5 13/14=6 .a=6 .b=6, gen(vote)
label define vote 1 Dilma 2 Aecio 3 Campos 4 Marina 5 Other 6 "Abstain/NR"
label values vote vote

recode vote 1=1 2/14=0 else=., gen(vote_dilma)
recode vote 2=1 1=0 3/14=0 else=., gen(vote_neves)
recode vote 3/4=1 1/2=0 5/14=0 else=., gen(vote_campos)
recode vote 5/6=1 1/4=0 else=., gen(vote_base)

	*Number of discussants
replace disc5b=. if disc5a==.a | disc5a==.b | disc5a==.c | disc5a==.
replace disc6b=. if disc6a==.a | disc6a==.b | disc6a==.c | disc6a==.

recode disc5a 1/8=1 9/10=0, gen(disc_family)
recode disc6a 1/8=1 10=0, gen(disc_friend)
egen disc_total=rsum(disc_family disc_friend)
replace disc_total=. if wave==1 | wave==6 | wave==7

	*discussants' vote intention
recode disc5b 1=1 2/14=0, gen(disc_family_dilma)
recode disc6b 1=1 2/14=0, gen(disc_friend_dilma)
egen disc_dilma=rsum(disc_family_dilma disc_friend_dilma)
replace disc_dilma=. if wave==1 | wave==6 | wave==7

recode disc5b 2=1 1=0 3/14=0, gen(disc_family_neves)
recode disc6b 2=1 1=0 3/14=0, gen(disc_friend_neves)
egen disc_neves=rsum(disc_family_neves disc_friend_neves)
replace disc_neves=. if wave==1 | wave==6 | wave==7

recode disc5b 5/8=1 1/4=0 9/14=0, gen(disc_family_other)
recode disc6b 5/8=1 1/4=0 9/14=0, gen(disc_friend_other)
egen disc_other=rsum(disc_family_other disc_friend_other)
replace disc_other=. if wave==1 | wave==6 | wave==7

		*note that this was Campos in wave B(wave 2), but Marina in C(wave 3) and thereafter 
recode disc5b 3/4=1 1/2=0 5/14=0, gen(disc_family_marina)
recode disc6b 3/4=1 1/2=0 5/14=0, gen(disc_friend_marina)
egen disc_marina=rsum(disc_family_marina disc_friend_marina)
replace disc_marina=. if wave==1 | wave==6 | wave==7

*Quick analyses for chapter 4 discussion about mechanisms
drop _m
merge m:1 idtelefone using "C:\Users\Andy Baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 4 Campaigns\Data\awarez 2014.dta" 

summ disc_neves disc_marina if (vote==6 | vote==1) & wave==5 & disc_total>0
summ disc_marina disc_dilma if (vote==6 | vote==2) & wave==5 & disc_total>0
summ disc_neves disc_dilma if (vote==6 | vote==4) & wave==5 & disc_total>0

summ disc_neves disc_marina if (vote==1) & wave==5 & disc_total>0
summ disc_marina disc_dilma if (vote==2) & wave==5 & disc_total>0
summ disc_neves disc_dilma if (vote==4) & wave==5 & disc_total>0
summ disc_dilma disc_marina disc_neves if (vote==6) & wave==5 & disc_total>0

tab disc_neves if vote==4 & wave==4 & awarez>.5, m
tab disc_neves if vote==4 & wave==5 & awarez>.5, m

tab disc_neves if vote==4 & wave==4 & awarez<-.5, m
tab disc_neves if vote==4 & wave==5 & awarez<-.5, m

tab disc_dilma if vote==4 & wave==4 & awarez>.5, m
tab disc_dilma if vote==4 & wave==5 & awarez>.5, m

tab disc_dilma if vote==4 & wave==4 & awarez<-.5, m
tab disc_dilma if vote==4 & wave==5 & awarez<-.5, m

tab disc_marina if vote==2 & wave==4 & awarez>.5, m
tab disc_marina if vote==2 & wave==5 & awarez>.5, m

tab disc_dilma if vote==2 & wave==4 & awarez<-.5, m
tab disc_dilma if vote==2 & wave==5 & awarez<-.5, m


	*party ID
recode vb11 (1501=1) (else=0), gen(partyintensePT1)
replace partyintensePT1=.5 if vb13==1501
replace partyintensePT1=. if wave~=1

recode vb11 (1503=1) (else=0), gen(partyintensePSDB1)
replace partyintensePSDB1=.5 if vb13==1503
replace partyintensePSDB1=. if wave~=1

recode vb11 (1504=1) (else=0), gen(partyintensePSB1)
replace partyintensePSB1=.5 if vb13==1504
replace partyintensePSB1=. if wave~=1

recode vb11 (1501=0) (1502=1) (1503/1504=0) (1505/1510=1) (.a=1) (.b=1) (.c=1), gen(partyintense_baseline)
replace partyintense_baseline=.5 if vb13==1502 
replace partyintense_baseline=.5 if vb13>1504 & vb13<1511
replace partyintense_baseline=0 if vb13==1501
replace partyintense_baseline=0 if vb13==1503
replace partyintense_baseline=0 if vb13==1504
replace partyintense_baseline=. if wave~=1

rename partyintensePT1 symp_pt
rename partyintensePSDB1 symp_psdb
rename partyintensePSB1 symp_psb
rename partyintense_baseline symp_base

/*
recode vb10 (.a=.) (.b=.) (.c=.)
recode vb11 (.a=.) (.b=.) (.c=.)
replace vb11=. if vb10==.
gen symp=vb11
replace symp=0 if vb10==2
replace symp=. if vb10==.

recode vb11 (1501=1) (else=0), gen(symp_pt)
recode vb11 (1503=1) (else=0), gen(symp_psdb)
recode vb11 (1504=1) (else=0), gen(symp_psb)
replace symp_pt=. if wave~=1
replace symp_psdb=. if wave~=1
replace symp_psb=. if wave~=1
*/


		*demographics
rename mulher woman
recode soct1 (1=5) (2=4) (3=3) (4=2) (5=1), gen(socio_current)
recode soct2 (1=3) (2=2) (3=1), gen(socio_change)
recode idio1 (1=5) (2=4) (3=3) (4=2) (5=1), gen(self_current)
recode spp1 2=0, gen(bf)
recode spp2 2=0, gen(tarifa_social)

recode pol1 (1=4) (2=3) (3=2) (4=1), gen(interest)
gen age=ln(p1_1)
recode urb_rur 2=0 1=1, gen(urban)

recode p2 1=1 .=. .a=.a .b=.b else=0, gen(white)
recode p2 2=1 .=. .a=.a .b=.b else=0, gen(brown)
recode p2 3=1 .=. .a=.a .b=.b else=0, gen(indigenous)
recode p2 4=1 .=. .a=.a .b=.b else=0, gen(black)
recode p2 5=1 .=. .a=.a .b=.b else=0, gen(asian)
replace educ=ln(educ)

drop wealth
recode hw1 hw2 hw3 hw4 hw5 hw6 p16 (2=0)
irt grm hw* p16 if wave==1
predict wealth if wave==1, latent ebmeans

		*issues: social issues not really working here
polychoricpca soc11 soc7 pp10 pp11 pp12 pp13 pp14, score(econ_issues) nscore(1)
impute econ_issues soc11 soc7 pp10 pp11 pp12 pp13 pp14 if wave==1, gen(econ_issues)

*polychoricpca abort rac2b d6b mil1, score(social_issues) nscore(1)
*rename social_issues1 social_issues

*Remember that any vars you want to use must be put in the next two commands
keep idtelefone wave respond vote disc_* p15 disc5a disc6a dilma neves silva municipio woman socio_current self_current bf tarifa_social white brown indigenous black asian educ wealth econ_issues symp_pt symp_psdb symp_psb symp_base vote_dilma vote_campos vote_neves vote_base interest urban age weight_combined dilma2014 aecio2014 marina2014 other2014 dilma2010 serra2010 marina2010 other2010 uf
reshape wide respond vote disc_* p15 bf disc5a disc6a socio_current self_current tarifa_social white brown indigenous black asian educ wealth econ_issues symp_pt symp_psdb symp_psb symp_base vote_dilma vote_campos vote_neves vote_base interest urban age weight_combined, i(idtelefone) j(wave)
	
gen weight_combined45=weight_combined4
replace weight_combined45=weight_combined5 if respond5==1

*DISCUSSANT IVS
	*Generate discussant variables
egen disc_total23=rsum(disc_total2 disc_total3)
replace disc_total23 =. if respond2==. & respond3==.
replace disc_total23 =. if respond2==1 & respond3==1

egen disc_total45=rsum(disc_total4 disc_total5)
replace disc_total45 =. if respond4==. & respond5==.
replace disc_total45 =. if respond4==1 & respond5==1

egen disc_dilma23=rsum(disc_dilma2 disc_dilma3)
replace disc_dilma23 =. if respond2==. & respond3==.
replace disc_dilma23 =. if respond2==1 & respond3==1

egen disc_dilma45=rsum(disc_dilma4 disc_dilma5)
replace disc_dilma45 =. if respond4==. & respond5==.
replace disc_dilma45 =. if respond4==1 & respond5==1

egen disc_neves23=rsum(disc_neves2 disc_neves3)
replace disc_neves23 =. if respond2==. & respond3==.
replace disc_neves23 =. if respond2==1 & respond3==1

egen disc_neves45=rsum(disc_neves4 disc_neves5)
replace disc_neves45 =. if respond4==. & respond5==.
replace disc_neves45 =. if respond4==1 & respond5==1

egen disc_marina23=rsum(disc_marina2 disc_marina3)
replace disc_marina23 =. if respond2==. & respond3==.
replace disc_marina23 =. if respond2==1 & respond3==1

egen disc_marina45=rsum(disc_marina4 disc_marina5)
replace disc_marina45 =. if respond4==. & respond5==.
replace disc_marina45 =. if respond4==1 & respond5==1

*Others mucks things up in the discussant choice model, so treating them as nonopinionated
egen disc_other23=rsum(disc_other2 disc_other3)
replace disc_other23 =. if respond2==. & respond3==.
replace disc_other23 =. if respond2==1 & respond3==1

egen disc_other45=rsum(disc_other4 disc_other5)
replace disc_other45 =. if respond4==. & respond5==.
replace disc_other45 =. if respond4==1 & respond5==1


*WAVE 4/5
	*strong definition
egen totopinionateddisc45=rsum(disc_dilma45 disc_neves45 disc_marina45 disc_other45)
replace totopinionateddisc45=. if respond4==. & respond5==.
replace totopinionateddisc45=. if respond4==1 & respond5==1

gen dilmadiscper45=disc_dilma45/totopinionateddisc45
gen nevesdiscper45=disc_neves45/totopinionateddisc45
gen marinadiscper45=disc_marina45/totopinionateddisc45
gen otherdiscper45=disc_other45/totopinionateddisc45
recode *discper45 (.=0)
recode totopinionateddisc45 0=1 1/3=0 .=., gen(zerodiscstrong45)

summ disc_neves5 disc_dilma5 disc_marina5 totopinionateddisc45 if (vote5==1)  & totopinionateddisc45>0
summ disc_neves5 disc_dilma5 disc_marina5 totopinionateddisc45 if (vote5==2)  & totopinionateddisc45>0
summ disc_neves5 disc_dilma5 disc_marina5 totopinionateddisc45 if (vote5==4)  & totopinionateddisc45>0
summ disc_neves5 disc_dilma5 disc_marina5 totopinionateddisc45 if (vote5==6)  & totopinionateddisc45>0


	*extra stuff for compositional analysis
replace dilmadiscper45 =. if totopinionateddisc45==.
replace nevesdiscper45 =. if totopinionateddisc45==.
replace marinadiscper45 =. if  totopinionateddisc45==.
replace otherdiscper45=. if  totopinionateddisc45==.

	*weak definition
gen dilmadiscperweak45=disc_dilma45/disc_total45
gen nevesdiscperweak45=disc_neves45/disc_total45
gen marinadiscperweak45=disc_marina45/disc_total45
gen nonediscperweak45=1-(disc_dilma45+disc_neves45+disc_marina45)/disc_total45

replace dilmadiscperweak45=0 if disc_total45==0 & dilmadiscperweak45==.
replace nevesdiscperweak45=0 if disc_total45==0 & nevesdiscperweak45==.
replace marinadiscperweak45=0 if disc_total45==0 & marinadiscperweak45==.
replace nonediscperweak45=0 if disc_total45==0 & nonediscperweak45==.
tab dilmadiscperweak45 dilmadiscper45, m
recode disc_total45 0=1 1/2=0 .=., gen(zerodiscweak45)

*REGS
label variable dilma2010 "PT's 2010 vote share in R’s state"
label variable marina2010 "Marina's 2010 vote share in R’s state"
label variable other2010 "Other parties' 2010 vote share in R’s state"
label variable symp_pt1 "Petista(pc wave)"
label variable symp_psb1 "Pessebista(pc wave)"
label variable symp_psdb1 "Pessedebista(pc wave)"
label variable symp_base1 "Partisans of other party and nonpartisans(pc wave)"
label variable vote_dilma1 "Vote intention for Rousseff(pc wave)"
label variable vote_neves1 "Vote intention for Neves(pc wave)"
label variable vote_campos1  "Vote intention for Campos(pc wave)"
label variable urban1 "Urban resident"
label variable educ1 "Education level"
label variable wealth1 "Wealth"
label variable nevesdiscper45 "Share of discussants that are pro-Neves"
label variable dilmadiscper45 "Share of discussants that are pro-Rousseff"
label variable marinadiscper45 "Share of discussants that are pro-Marina"
label variable woman "Woman"

*Discussant Choice
local SerraMA=.151/(.136+.71127+.151)
local MarinaMA=.136/(.136+.71127+.151)
local otherMA=0

local SerraSC=.4619/(.4619+.1406+.3931)
local MarinaSC=.1406/(.4619+.1406+.3931)
local otherSC=0

local SerraRR=.5102/(.5102+.18769+.287)
local MarinaRR=.18769/(.5102+.18769+.287)
local otherRR=0


*Strong definition of disagreement	
eststo clear
*Figure 6.6 and Table A.15
eststo: fmlogit dilmadiscper45 nevesdiscper45 marinadiscper45 otherdiscper45 if zerodiscstrong45==0 [pweight=weight_combined45] , ///
etavar(serra2010 marina2010 other2010 symp_psdb1 symp_psb1 symp_base1 vote_neves1 vote_campos1 vote_base1 urban1 educ1 wealth1 woman) cluster(uf)

margins , at(serra2010=`SerraRR' marina2010=`MarinaRR' other2010=`otherRR' (mean) symp_psdb1 symp_psb1 symp_base1 vote_neves1 vote_campos1 vote_base1 urban1 wealth1 educ1 woman) predict(outcome(nevesdiscper45 )) predict(outcome(dilmadiscper45 )) predict(outcome(marinadiscper45)) 
margins , at(serra2010=`SerraSC' marina2010=`MarinaSC' other2010=`otherSC' (mean) symp_psdb1 symp_psb1 symp_base1 vote_neves1 vote_campos1 vote_base1 urban1 wealth1 educ1 woman) predict(outcome(nevesdiscper45 )) predict(outcome(dilmadiscper45 )) predict(outcome(marinadiscper45)) 
margins , at(serra2010=`SerraMA' marina2010=`MarinaMA' other2010=`otherMA' (mean) symp_psdb1 symp_psb1 symp_base1 vote_neves1 vote_campos1 vote_base1 urban1 wealth1 educ1 woman) predict(outcome(nevesdiscper45 )) predict(outcome(dilmadiscper45 )) predict(outcome(marinadiscper45)) 
margins , at(serra2010=.232 marina2010=.42 other2010=0 (mean) symp_psdb1 symp_psb1 symp_base1 vote_neves1 vote_campos1 vote_base1 urban1 wealth1 educ1 woman) predict(outcome(nevesdiscper45 )) predict(outcome(dilmadiscper45 )) predict(outcome(marinadiscper45)) 
margins , at(serra2010=.356 marina2010=.11 other2010=0 (mean) symp_psdb1 symp_psb1 symp_base1 vote_neves1 vote_campos1 vote_base1 urban1 wealth1 educ1 woman) predict(outcome(nevesdiscper45 )) predict(outcome(dilmadiscper45 )) predict(outcome(marinadiscper45))

cd "C:\Users\Andy Baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 6 Regionalism\Code\Output\"
esttab est1 using Brazil2014_disc_choice.rtf, label b(3) replace se star(* 0.05 ) nogap onecell ///
title(Table . The Correlates of Discussant Choice in Brazil 2014) ///
modelwidth(6) compress ///
addnote("Entries are quasi maximum likelihood fractional multinomial logit coefficients with robust standard errors (corrected for clustering by state) in parentheses.")

recode vote6 5=.

*Table 6.2 and Table A.17
quietly mlogit vote6 serra2010 marina2010 other2010 vote_neves1 vote_campos1 vote_base1 wealth1 educ1 bf1 econ_issues1 urban1 woman symp_psdb1 symp_psb1 symp_base1 nevesdiscper45 marinadiscper45 otherdiscper45 zerodiscstrong45 [pweight=weight_combined45], cluster(uf) b(1) 
gen esample=1 if e(sample)==1

eststo clear
eststo: mlogit vote6 serra2010 marina2010 other2010 vote_neves1 vote_campos1 vote_base1 if esample==1 [pweight=weight_combined45], b(1) cluster(uf)
eststo: mlogit vote6 serra2010 marina2010 other2010 vote_neves1 vote_campos1 vote_base1 wealth1 educ1 bf1 econ_issues1 urban1 woman symp_psdb1 symp_psb1 symp_base1 if esample==1 [pweight=weight_combined45], b(1) cluster(uf)
eststo: mlogit vote6 serra2010 marina2010 other2010 vote_neves1 vote_campos1 vote_base1 wealth1 educ1 bf1 econ_issues1 urban1 woman symp_psdb1 symp_psb1 symp_base1 nevesdiscper45 marinadiscper45 otherdiscper45 zerodiscstrong45 if esample==1 [pweight=weight_combined45], b(1) cluster(uf)

esttab est1 est2 est3 using Brazil2014_mnls.rtf, label b(3) replace se star(* 0.05 ) nogap onecell ///
title(Table 6.x. Correlates of Presidential Vote Choice in Brazil, 2006) ///
modelwidth(6) compress ///
addnote("Entries are multinomial logit coefficients and standard errors (adjusted for clustering within state) in parentheses. Coefficients for three other dependent variable categories (vote for other and abstention) are not reported to reduce clutter.")
*/
xyz






*For appendix: weak definition
eststo clear
eststo: fmlogit nevesdiscperweak45 dilmadiscperweak45 marinadiscperweak45 nonediscperweak45 if zerodiscweak45==0 [pweight=weight_combined45] , ///
etavar(dilma2010 marina2010 other2010 symp_pt1 symp_psb1 symp_base1 vote_dilma1 vote_campos1 vote_base1 urban1 educ1 wealth1 woman) cluster(uf)

margins , at(dilma2010=`DilmaRR' marina2010=`MarinaRR' other2010=0 (mean) symp_pt1 symp_psb1 symp_base1 vote_dilma1 vote_campos1 vote_base1 urban1 wealth1 educ1 woman) predict(outcome(nevesdiscperweak45 )) predict(outcome(dilmadiscperweak45 )) predict(outcome(marinadiscperweak45)) predict(outcome(nonediscperweak45)) 
margins , at(dilma2010=`DilmaSC' marina2010=`MarinaSC' other2010=0 (mean) symp_pt1 symp_psb1 symp_base1 vote_dilma1 vote_campos1 vote_base1  urban1 wealth1 educ1 woman) predict(outcome(nevesdiscperweak45 )) predict(outcome(dilmadiscperweak45 )) predict(outcome(marinadiscperweak45)) predict(outcome(nonediscperweak45)) 
margins , at(dilma2010=`DilmaMA' marina2010=`MarinaMA' other2010=0 (mean) symp_pt1 symp_psb1 symp_base1 vote_dilma1 vote_campos1 vote_base1  urban1 wealth1 educ1 woman) predict(outcome(nevesdiscperweak45 )) predict(outcome(dilmadiscperweak45 )) predict(outcome(marinadiscperweak45)) predict(outcome(nonediscperweak45))

cd "C:\Users\Andy Baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 6 Regionalism\Code\Output\"
esttab est1 using Brazil2014_disc_choice_weak.rtf, label b(3) replace se star(* 0.05 ) nogap onecell ///
title(Table 5.3A. The Correlates of Discussant Choice in Brazil 2014, permissive definition) ///
modelwidth(6) compress ///
addnote("Entries are quasi maximum likelihood fractional multinomial logit coefficients with robust standard errors (corrected for clustering by state) in parentheses.")

eststo: mlogit vote6 dilma2010 marina2010 other2010 wealth1 educ1 bf1 econ_issues1 urban1 woman symp_pt1 symp_psb1 symp_base1 dilmadiscperweak45 marinadiscperweak45 zerodiscweak45 if esample==1 [pweight=weight_combined45], cluster(uf) b(2)

esttab est1 using Brazil2014_mnls_weak.rtf, label b(3) replace se star(* 0.05 ) nogap onecell ///
title(Table 5.5A. Correlates of Presidential Vote Choice in Mexico, 2006 using permissive definition) ///
modelwidth(6) compress ///
addnote("Entries are multinomial logit coefficients and standard errors (adjusted for clustering within state) in parentheses. Coefficients for three other dependent variable categories (vote for other and abstention) are not reported to reduce clutter.")

xyz

